﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TYBase;
using TYControl;
using TYPLC;

namespace TYComm_For_DSApp.View
{
    public partial class View_PLC_Control_SendArray : WindowT
    {
        private readonly int count;

        public int[] SendData { get; set; }
        public PLCDataType DataType { get; }

        public View_PLC_Control_SendArray(PLCDataType dataType, int count)
        {
            InitializeComponent();
            DataType = dataType;
            this.count = count;
            SendData = new int[count];
            List<string> listShow = new List<string>();
            for (int i = 0; i < count; i++)
            {
                listShow.Add("");
            }
            txt.Text = String.Join(",", listShow);
        }

        private void BtnClick_Send_0(object sender, RoutedEventArgs e)
        {
            var text = txt.Text.Replace(" ", "").Replace("，", ",");
            var listData = text.Split(',');
            List<int> listData_tmp = new List<int>();
            foreach (var item in listData)
            {
                if (item.NotNull())
                {
                    var i_tmp = item.AsInt();
                    if (DataType == PLCDataType.BOOL)
                    {
                        if (i_tmp != 0 || i_tmp != 1)
                        {
                            MessageBox.Show("bool下请确认0/1");
                            return;
                        }
                    }
                    listData_tmp.Add(item.AsInt());
                }
            }
            if (listData_tmp.Count != count)
            {
                MessageBox.Show($"当前长度不对! 需要{count},输入的是 {listData_tmp.Count}");
                return;
            }
            for (int i = 0; i < count; i++)
            {
                SendData[i] = listData_tmp[i];
            }
            this.DialogResult = true;
        }
    }
}